package com.easy.query.solon.web.controller;

import com.easy.query.api4j.client.EasyQuery;
import com.easy.query.core.annotation.EasyQueryTrack;
import com.easy.query.solon.annotation.Db;
import com.easy.query.solon.web.domain.SysUser;
import com.easy.query.solon.web.domain.SysUserSM4;
import com.easy.query.solon.web.domain.UserBook;
import com.easy.query.solon.web.vo.UserBookVO;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.AopContext;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.data.annotation.Tran;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * create time 2023/8/12 21:45
 * 文件说明
 *
 * @author xuejiaming
 */
@Controller
@Mapping("/test")
public class TestController {
    @Db
    private EasyQuery easyQuery;
    @Inject
    private AopContext aopContext;
    @Mapping(value = "/init",method = MethodType.GET)
    @Tran
    public String init(){
        {

            SysUser sysUser = new SysUser();
            sysUser.setId("1");
            sysUser.setName("用户1");
            sysUser.setPhone("12345678901");
            sysUser.setAddress("浙江省绍兴市越城区城市广场1234号");
            sysUser.setCreateTime(LocalDateTime.now());
            ArrayList<UserBook> userBooks = new ArrayList<>();
            UserBook userBook = new UserBook();
            userBook.setId("1");
            userBook.setUserId("1");
            userBook.setName("语文");
            userBooks.add(userBook);
            UserBook userBook1 = new UserBook();
            userBook1.setId("2");
            userBook1.setUserId("1");
            userBook1.setName("数学");
            userBooks.add(userBook1);
            easyQuery.insertable(sysUser).executeRows();
            easyQuery.insertable(userBooks).executeRows();
        }
        {

            SysUser sysUser = new SysUser();
            sysUser.setId("2");
            sysUser.setName("用户2");
            sysUser.setPhone("19012345678");
            sysUser.setAddress("浙江省杭州市上城区武林广场1234号");
            sysUser.setCreateTime(LocalDateTime.now());
            ArrayList<UserBook> userBooks = new ArrayList<>();
            UserBook userBook = new UserBook();
            userBook.setId("3");
            userBook.setUserId("2");
            userBook.setName("语文");
            userBooks.add(userBook);
            UserBook userBook1 = new UserBook();
            userBook1.setId("4");
            userBook1.setUserId("2");
            userBook1.setName("英语");
            userBooks.add(userBook1);
            easyQuery.insertable(sysUser).executeRows();
            easyQuery.insertable(userBooks).executeRows();
        }
        return "初始化完成";
    }

    @Mapping(value = "/query",method = MethodType.GET)
    public Object query(){
        List<SysUser> list = easyQuery.queryable(SysUser.class)
                .include(o -> o.many(SysUser::getBooks))
                .toList();
        return list;
    }
    @Mapping(value = "/queryByAddress",method = MethodType.GET)
    public Object queryByAddress(){
        SysUser user = easyQuery.queryable(SysUser.class)
                .where(o->o.like(SysUser::getAddress,"越城区"))
                .firstOrNull();
        return user;
    }
    @Mapping(value = "/queryBook",method = MethodType.GET)
    public Object queryBook(){
        List<UserBookVO> userBooks = easyQuery.queryable(UserBook.class)
                .leftJoin(SysUser.class, (t, t1) -> t.eq(t1, UserBook::getUserId, SysUser::getId))
                .where((t, t1) -> t1.like(SysUser::getAddress, "越城区"))
                .select(UserBookVO.class, (t, t1) -> t.columnAll()
                        .then(t1)
                        .columnAs(SysUser::getName, UserBookVO::getUserName)
                        .columnAs(SysUser::getPhone, UserBookVO::getUserPhone)
                        .columnAs(SysUser::getAddress, UserBookVO::getUserAddress)
                )
                .toList();
        return userBooks;
    }
    @Mapping(value = "/queryBookMask",method = MethodType.GET)
    public Object queryBookMask(){
        List<UserBookVO> userBooks = easyQuery.queryable(UserBook.class)
                .leftJoin(SysUser.class, (t, t1) -> t.eq(t1, UserBook::getUserId, SysUser::getId))
                .where((t, t1) -> t1.like(SysUser::getAddress, "越城区"))
                .select(UserBookVO.class, (t, t1) -> t.columnAll()
                        .then(t1)
                        .columnAs(SysUser::getName, UserBookVO::getUserName)
                        .columnAs(SysUser::getPhone, UserBookVO::getUserPhone)
                        .columnAs(SysUser::getAddress, UserBookVO::getUserAddress)
                )
                .toList();
        return userBooks;
    }
    @Mapping(value = "/updateFull",method = MethodType.GET)
    public Object updateFull(){
        SysUser user = easyQuery.queryable(SysUser.class).whereById("1").firstOrNull();
        user.setPhone("12312312312");
        user.setAddress("浙江省杭州市上城区0987号");
        easyQuery.updatable(user).executeRows();
        return null;
    }
    @Mapping(value = "/updateTrack",method = MethodType.GET)
    @EasyQueryTrack
    public Object updateTrack(){
        SysUser user = easyQuery.queryable(SysUser.class).whereById("1").firstOrNull();
        user.setPhone("123123123123");
        user.setAddress("绍兴市越城区87651号");
        easyQuery.updatable(user).executeRows();
        return null;
    }
    @Mapping(value = "/updateSet",method = MethodType.GET)
    public Object updateSet(){
        easyQuery.updatable(SysUser.class)
                .set(SysUser::getPhone,"1234567")
                .set(SysUser::getAddress,"绍兴市越城区123号")
                .whereById("1")
                .executeRows();
        return null;
    }
    @Mapping(value = "/queryByPhone",method = MethodType.GET)
    public Object queryByPhone(){
        SysUser sysUser = easyQuery.queryable(SysUser.class)
                .where(o -> o.eq(SysUser::getPhone,"1234567"))
                .firstOrNull();
        return sysUser;
    }
    @Mapping(value = "/queryPhone",method = MethodType.GET)
    public Object queryPhone(){
        SysUser sysUser = easyQuery.queryable(SysUser.class)
                .where(o -> o.sqlNativeSegment("AES_DECRYPT(from_base64({0}),{1}) like {2}",context->{
                    context.expression(SysUser::getPhone)//列
                            .value("1234567890123456")//秘钥
                            .value("%123%");//like值
                }))
                .firstOrNull();
        return sysUser;
    }
//    @Mapping(value = "/queryPhone1",method = MethodType.GET)
//    public Object queryPhone1(){
//        SysUser sysUser = easyQuery.queryable(SysUser.class)
//                .where(o -> o.like(SysUser::getPhone, "123"))
//                .firstOrNull();
//        return sysUser;
//    }


    @Mapping(value = "/initSM4",method = MethodType.GET)
    @Tran
    public String initSM4(){
        {

            SysUserSM4 sysUser = new SysUserSM4();
            sysUser.setId("5");
            sysUser.setName("用户5");
            sysUser.setPhone("12345678901");
            sysUser.setAddress("浙江省绍兴市越城区城市广场1234号");
            sysUser.setCreateTime(LocalDateTime.now());
            easyQuery.insertable(sysUser).executeRows();
        }
        {

            SysUserSM4 sysUser = new SysUserSM4();
            sysUser.setId("6");
            sysUser.setName("用户6");
            sysUser.setPhone("19012345678");
            sysUser.setAddress("浙江省杭州市上城区武林广场1234号");
            sysUser.setCreateTime(LocalDateTime.now());
            easyQuery.insertable(sysUser).executeRows();
        }
        return "初始化完成";
    }
    @Mapping(value = "/querySM4",method = MethodType.GET)
    public Object querySM4(){
        List<SysUserSM4> list = easyQuery.queryable(SysUserSM4.class)
                .whereByIds(Arrays.asList("5", "6"))
                .toList();
        return list;
    }
    @Mapping(value = "/queryLikeSM4",method = MethodType.GET)
    public Object queryLikeSM4(){
        List<SysUserSM4> list = easyQuery.queryable(SysUserSM4.class)
                .whereByIds(Arrays.asList("5", "6"))
                .where(o->o.like(SysUserSM4::getAddress,"武林广场"))
                .toList();
        return list;
    }

}
